package com.taru.university.controller;

import com.taru.university.utils.Constant;
import com.taru.university.utils.JsonResult;
import com.taru.university.model.Comment;
import com.taru.university.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@CrossOrigin
public class CommentController {
    private static Logger logger = LoggerFactory.getLogger(CommentController.class);
    @Autowired
    CommentService commentService;

    /**
     * 查询评论
     * @param articleId
     * @return
     */
    @RequestMapping("/query/comment/details")
    @ResponseBody
    public Object queryCommentByArticleId(String articleId){
        JsonResult result = null;
        try{
            List<Comment> commentList = commentService.queryCommentByArticleId(articleId);
            logger.info("帖子id：{}",articleId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, commentList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    /**
     * 插入评论
     * @param comment
     * @return
     */
    @RequestMapping("/insert/comment")
    @ResponseBody
    public Object insertComment(Comment comment){
        JsonResult result = null;
        try{

                commentService.insertComment(comment);
                logger.info("评论内容：{}",comment.toString());
                result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);

        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    /**
     * 删除评论
     * @param userId
     * @param articleId
     * @return
     */
    @RequestMapping("/delete/comment")
    @ResponseBody
    public Object deleteComment(String userId, String articleId){
        JsonResult result = null;
        try{
            commentService.deleteComment(userId, articleId);
            logger.info("用户id：{},帖子id{}" ,userId, articleId);
            result = new JsonResult("删除成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("删除异常", Constant.STATUC_FAILURE);
        }
        return result;
    }
}
